package com.sailfish.springbootdemo.controller.db1;

import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.Upkeep;
import com.sailfish.springbootdemo.service.db1.UpkeepService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/upkeep")
public class UpkeepController {

    @Autowired
    private UpkeepService upkeepService;

    @PostMapping("/addUpkeep")
    public Result addUpkeep(Upkeep upkeep) {
        return upkeepService.addUpkeep(upkeep);
    }

    @PostMapping("/deleteUpkeep")
    public Result deleteUpkeep(Integer upkeepId) {
        return upkeepService.deleteUpkeep(upkeepId);
    }

    @PostMapping("/updateUpkeep")
    public Result updateUpkeep(Upkeep upkeep) {
        try {
            if (upkeep.getUpkeepId() == null || "".equals(upkeep.getUpkeepId().toString())) {
                return ResultUtil.error(500, "缺少参数upkeepId", "Miss Param upkeepId", null);
            }
            Boolean b = upkeepService.updateUpkeep(upkeep);
            if (b) {
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @PostMapping("/getUpkeepByAssetNumber")
    public Result getUpkeepByAssetNumber(String assetNumber) {
        try {
            if (assetNumber == null || "".equals(assetNumber)) {
                return ResultUtil.error(500, "缺少参数assetNumber", "Miss Param assetNumber", null);
            }
            return ResultUtil.success(upkeepService.getUpkeepByAssetNumber(assetNumber));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
